package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.floatingmenu.MenuItem;
import com.eqdd.floatingmenu.TopRightMenu;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.UserCardActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.UserCardInfoResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rongyun.message.mingpian.Content;
import com.eqdd.yiqidian.rongyun.message.mingpian.MingpianMessage;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.IMUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

public class UserCardActivity extends BaseActivity {
    private UserCardActivityCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;
    private String guid;
    private ArrayList<Object> datas;
    private String name;
    private UserCardInfoResultBean items;
    private TopRightMenu mTopRightMenu;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_card);
        initTopTitleBar(View.VISIBLE, "个人名片");
        initTopRightText("更多", v -> {
            mTopRightMenu = new TopRightMenu(UserCardActivity.this);

//添加菜单项
            List<MenuItem> menuItems = new ArrayList<>();
            menuItems.add(new MenuItem("工作圈权限"));
            menuItems.add(new MenuItem("发名片"));
            menuItems.add(new MenuItem("删除"));

            mTopRightMenu
                    .setHeight(300)     //默认高度480
                    .setWidth(250)      //默认宽度wrap_content
                    .showIcon(false)     //显示菜单图标，默认为true
                    .dimBackground(true)        //背景变暗，默认为true
                    .needAnimationStyle(true)   //显示动画，默认为true
                    .setAnimationStyle(R.style.TRM_ANIM_STYLE)
                    .addMenuList(menuItems)
                    .top(TopRightMenu.Align.RIGHT, 10, 15)
                    .setOnMenuItemClickListener(position -> {
                        switch (position) {
                            case 0:
                                Bundle bundle = new Bundle();
                                bundle.putString("friendGuid", guid);
                                startActivity(FriendCircleSettingActivity.class, bundle);
                                break;
                            case 1:
                                ShareConf shareConf = new ShareConf().setTitle("请选择")
                                        .setRightTitle("发送")
                                        .setMaxNum(5)
                                        .setMaxTip("最多%d人");

                                ShareEnterConf shareEnterConf = (ShareEnterConf) new ShareEnterConf()
                                        .setFriendConf(shareConf)
                                        .setQunChooseConf(shareConf)
                                        .setQunListConf(shareConf)
                                        .setQunMemberConf(shareConf)
                                        .setStaffConf(shareConf)
                                        .setShare(false)
                                        .setAll(false)
                                        .setTitle("请选择")
                                        .setMaxNum(5)
                                        .setRightTitle("发送")
                                        .setMaxTip("最多%d人");
                                Bundle bundle1 = new Bundle();
                                bundle1.putSerializable("bean", shareEnterConf);
                                startActivityForResult(ShareEnterActivity.class, bundle1, Config.SHARE_ENTER);
                                break;
                            case 2:
                                delete();
                                break;
                        }
                    })
                    .showAsDropDown(v, 8, -20);    //带偏移量
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.SHARE_ENTER) {
            if (resultCode == Config.SUCCESS) {
                ArrayList<SelectBean> selectBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
                for (SelectBean selectBean : selectBeans) {
                    if (selectBean.getType() == Conversation.ConversationType.PRIVATE) {
                        Content content = new Content(items.getUname(), items.getPhoto(),
                                items.getDepartment(), items.getPost(),
                                items.getCompany(), items.getUserGuid(), "0");

                        MingpianMessage obtain = MingpianMessage.obtain(content);
                        Message message = Message.obtain(selectBean.getGuid(), selectBean.getType(), obtain);
                        RongIM.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
                            @Override
                            public void onAttached(Message message) {

                            }

                            @Override
                            public void onSuccess(Message message) {
                                System.out.println("发送成功");
                            }

                            @Override
                            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                System.out.println(errorCode.toString());
                            }

                        });
                    }
                }
            }
        }
    }

    private void delete() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_DELETE)
                .params("userGuid", AppConstent.user.getGuid())
                .params("friendGuid", guid)
                .execute(new DialogCallBack<HttpResult>(UserCardActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            finish();
                        }
                    }
                });
    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        guid = getIntent().getStringExtra(Config.GUID);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_39, new SlimInjector<SlimEditHintBean>() {
            @Override
            public void onInject(SlimEditHintBean data, IViewInjector injector) {
                injector.text(R.id.tv_left, data.getTitle())
                        .text(R.id.tv_right, data.getContent());
            }
        }).register(R.layout.list_item_32, new SlimInjector<Integer>() {
            @Override
            public void onInject(Integer data, IViewInjector injector) {

            }
        }).register(R.layout.list_item_40, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_center, data);
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position) instanceof String) {
                        IMUtil.startPrivate(UserCardActivity.this, guid, name, items.getPhoto());

                    }
                });
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<UserCardInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.USER_CARD_INFO)
                .params("userGuid", guid)
                .execute(new DialogCallBack<HttpResult<UserCardInfoResultBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<UserCardInfoResultBean>> response) {
                        HttpResult<UserCardInfoResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            datas.clear();
                            datas.add(50);
                            datas.add(new SlimEditHintBean("手机号", items.getUname(), 1));
                            datas.add(new SlimEditHintBean("地区", items.getLocation(), 1));
                            datas.add(50);
                            datas.add(new SlimEditHintBean("公司", items.getCompany(), 1));
                            datas.add(new SlimEditHintBean("部门职位", items.getDepartment() + "/" + items.getPost(), 1));
                            datas.add(50);
                            datas.add("发消息");
                            slimAdapterEx.updateData(datas);
                            ImageUtil.setCircleImage(items.getPhoto(), dataBinding.ivHead);
                            dataBinding.tvTop.setText(items.getUpname());
                            name = items.getUpname();
                            dataBinding.tvDown.setText("易企点号：" + (TextUtils.isEmpty(items.getEQDCode()) ? "暂无" : items.getEQDCode()));
                        } else {
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_erweima:
                Bundle bundle = new Bundle();
                bundle.putString(Config.GUID, guid);
                bundle.putString(Config.NAME, name);
                startActivity(UserErweimaActivity.class, bundle);
                break;
        }
    }
}
